home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Question on 'bubble sort'
- Date: Sat, 30 Mar 96 17:29:18 GMT
- Organization: none
- Message-ID: <828206958snz@genesis.demon.co.uk>
- References: <4jieso$juc@lantana.singnet.com.sg>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jieso$juc@lantana.singnet.com.sg>
- s8700055@singnet.com.sg "XY Xie" writes:
-
- >I came across this sorting algorithm called 'bubble sort' in a book.
-
- If the book doesn't explain why there is never a good reason to use bubble
- sort then I suggest you get another book.
-
- >Here is a part of the book's code for sorting an array of integers in
- >ascending order :
- >
- >for(outer=0 ; outer<(NUM-1) ; outer++)
- >{
- > //variable definitions here......
-
- If that book suggests that you use // in C code then definitely get a
- different one. // is a syntax error in C.
-
-
- > int didswap = 0; //didswap remains zero if no swap occurs below
- > //my question is on 'didswap'
- >
- >
- >for (inner=outer ; inner< NUM ; inner++)
- > {
- > if ( arrayname[outer] > arrayname[inner])
- > {
- > //code that swaps values follows
- > didswap = 1 //didswap = 1 if a swap took place
- > }
- > }
- >
- >if ( ! didswap) //*returns 1 if swap took place*(relates to question)
- >
- > { break ; } //outer for loop terminated
- >
- >}
- >
- >My QUESTION : if the smallest integer happens to be the first member
- >initially , would not the 'if ( !didswap ) ' test terminate the
- >sorting procedure prematurely , with the rest of the list unsorted ?
- >If not , why ?
-
- didswap will be set to 1 if any swap occurred in the inner loop. If the
- first element is the smallest didswap won't be set to 1 on the first
- iteration of the inner loop but it may still be set on subsequent iterations.
- if (!didswap) would only succeed if no swaps occurred.
-
- >Someone pls enlighten me . Pls email me direct . THANKs for the
- >trouble .
-
- If you post to a newsgroup have the courtesy to read the responses there
- so that others can benefit from them and comment on them (that is in your
- interest).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-